package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.ApiConstants;
import cn.glinks.liuhe.api.ApiRequest;
import cn.glinks.liuhe.api.jsonbean.PersonalInfo;
import cn.glinks.liuhe.api.jsonbean.UserInfo;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.baserx.RxSubscriber;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.FormatUtils;
import cn.glinks.liuhe.utils.GsonUtils;
import cn.glinks.liuhe.utils.SoftInputUtils;
import cn.glinks.liuhe.utils.ToastUtils;
import cn.glinks.liuhe.widget.pickerview.FlatOptionsPickerView;
import cn.glinks.liuhe.widget.pickerview.FlatTimePickerView;
import cn.glinks.liuhe.widget.pickerview.PickerViewDataLoader;

/**
 * Des:填写资料页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class FillInfoActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTitleText;

    @BindView(R.id.et_name)// 姓名
    EditText mNameEdit;

    @BindView(R.id.et_sex)// 性别
    EditText mSexEdit;

    @BindView(R.id.et_birthday)// 出生年月
    EditText mBirthdayEdit;

    @BindView(R.id.et_native_place)// 籍贯
    EditText mNativePlaceEdit;

    @BindView(R.id.et_area)// 所属园区
    EditText mAreaEdit;

    @BindView(R.id.et_race)// 民族
    EditText mRaceEdit;

    @BindView(R.id.et_marital)// 婚姻
    EditText mMaritalEdit;

    @BindView(R.id.et_id)// 身份证号
    EditText mIdEdit;

    @BindView(R.id.et_phone)// 手机号
    EditText mPhoneEdit;

    @BindView(R.id.et_education)// 学历
    EditText mEducationEdit;

    @BindView(R.id.et_graduate_time)// 毕业时间
    EditText mGraduateTimeEdit;

    @BindView(R.id.et_graduate_school)// 毕业院校
    EditText mGraduateSchoolEdit;

    @BindView(R.id.et_major)// 所学专业
    EditText mMajorEdit;

    @BindView(R.id.et_life_experience)// 成长经历
    EditText mLifeExperienceEdit;

    // 变量
    private UserInfo mUserInfo;
    private String mNameStr;
    private String mSexStr;
    private String mBirthdayStr;
    private String mNativePlaceStr;
    private String mAreaStr;
    private String mRaceStr;
    private String mMaritalStr;
    private String mIdStr;
    private String mPhoneStr;
    private String mEducationStr;
    private String mGraduateTimeStr;
    private String mGraduateSchoolStr;
    private String mMajorStr;
    private String mLifeExperienceStr;

    // 选择器数据缓存
    private ArrayList<String> mSexList;
    private ArrayList<String> mNativePlaceList;
    private ArrayList<String> mAreaList;
    private ArrayList<String> mRaceList;
    private ArrayList<String> mMaritalList;
    private ArrayList<String> mEducationList;

    // 选择器PickerView
    private FlatTimePickerView mBirthdayFTPV;
    private FlatTimePickerView mGraduateTimeFTPV;
    private FlatOptionsPickerView mSexFOPV;
    private FlatOptionsPickerView mNativePlaceFOPV;
    private FlatOptionsPickerView mAreaFOPV;
    private FlatOptionsPickerView mRaceFOPV;
    private FlatOptionsPickerView mMaritalFOPV;
    private FlatOptionsPickerView mEducationFOPV;

    @Override
    public int getLayoutId() {
        return R.layout.activity_fill_info;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 加载数据
        initOptionData();
        // 加载用户信息
        mUserInfo = ((AppApplication)getApplicationContext()).getLoginUser();
        // 设置为透明状态栏
        SetTranslanteBar();
        // 初始化标题
        mTitleText.setText("填写资料");
        // 禁用一些EditText控件的获取焦点功能,自动填写或使用选择器
        mNameEdit.setFocusable(true);
        mSexEdit.setFocusable(false);
        mBirthdayEdit.setFocusable(false);
        mNativePlaceEdit.setFocusable(false);
        mAreaEdit.setFocusable(false);
        mRaceEdit.setFocusable(false);
        mMaritalEdit.setFocusable(false);
        mIdEdit.setFocusable(false);
        mPhoneEdit.setFocusable(false);
        mEducationEdit.setFocusable(false);
        mGraduateTimeEdit.setFocusable(false);
        mGraduateSchoolEdit.setFocusable(true);
        mMajorEdit.setFocusable(true);
        mLifeExperienceEdit.setFocusable(true);
        // 自动填入身份证号和手机号信息
        //mIdEdit.setText(mUserInfo.getUser().getIdNo());
        mPhoneEdit.setText(mUserInfo.getUser().getPhoneNo());
    }

    /**
     * Des:加载选择器全部数据
     */
    private void initOptionData() {
        mSexList = PickerViewDataLoader.loadSex();
        mNativePlaceList = PickerViewDataLoader.loadNativePlace();
        mAreaList = PickerViewDataLoader.loadArea();
        mRaceList = PickerViewDataLoader.loadRace();
        mMaritalList = PickerViewDataLoader.loadMarital();
        mEducationList = PickerViewDataLoader.loadEducation();
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 选择出生年月
    @OnClick(R.id.et_birthday)
    public void clickSelectBirthday() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mBirthdayFTPV) {
            mBirthdayFTPV = buildBirthdayPickerView();
        }
        mBirthdayFTPV.show();
    }

    // 选择出生年月
    @OnClick(R.id.et_graduate_time)
    public void clickSelectGraduateTime() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mGraduateTimeFTPV) {
            mGraduateTimeFTPV = buildGraduateTimePickerView();
        }
        mGraduateTimeFTPV.show();
    }

    // 选择性别
    @OnClick(R.id.et_sex)
    public void clickSelectSex() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mSexFOPV) {
          mSexFOPV = buildSexPickerView();
        }
        mSexFOPV.show();
    }

    // 选择民族
    @OnClick(R.id.et_race)
    public void clickSelectRace() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mRaceFOPV) {
            mRaceFOPV = buildRacePickerView();
        }
        mRaceFOPV.show();
    }

    // 选择学历
    @OnClick(R.id.et_education)
    public void clickSelectEducation() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mEducationFOPV) {
            mEducationFOPV = buildEducationPickerView();
        }
        mEducationFOPV.show();
    }

    // 选择籍贯
    @OnClick(R.id.et_native_place)
    public void clickSelectNativePlace() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mNativePlaceFOPV) {
            mNativePlaceFOPV = buildNativePlacePickerView();
        }
        mNativePlaceFOPV.show();
    }

    // 选择所属园区
    @OnClick(R.id.et_area)
    public void clickSelectArea() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mAreaFOPV) {
            mAreaFOPV = buildAreaPickerView();
        }
        mAreaFOPV.show();
    }

    // 选择婚姻
    @OnClick(R.id.et_marital)
    public void clickSelectMarital() {
        SoftInputUtils.hideSoftKeyboard(this);

        if (null == mMaritalFOPV) {
            mMaritalFOPV = buildMaritalPickerView();
        }
        mMaritalFOPV.show();
    }

    // 提交个人资料
    @OnClick(R.id.btn_confirm)
    public void clickCommitInfo() {
        // 获取数据
        mNameStr = mNameEdit.getText().toString().trim();
        mSexStr = mSexEdit.getText().toString().trim();
        mBirthdayStr = mBirthdayEdit.getText().toString().trim();
        mNativePlaceStr = mNativePlaceEdit.getText().toString().trim();
        mAreaStr = mAreaEdit.getText().toString().trim();
        mRaceStr = mRaceEdit.getText().toString().trim();
        mMaritalStr = mMaritalEdit.getText().toString().trim();
        mIdStr = mIdEdit.getText().toString().trim();
        mPhoneStr = mPhoneEdit.getText().toString().trim();
        mEducationStr = mEducationEdit.getText().toString().trim();
        mGraduateTimeStr = mGraduateTimeEdit.getText().toString().trim();
        mGraduateSchoolStr = mGraduateSchoolEdit.getText().toString().trim();
        mMajorStr = mMajorEdit.getText().toString().trim();
        mLifeExperienceStr = mEducationEdit.getText().toString().trim();
        // 检查数据有效性
        if (mNameStr.isEmpty()) {
            ToastUtils.showShort("请填写姓名");
            return;
        }
        if (mSexStr.isEmpty()) {
            ToastUtils.showShort("请选择性别");
            return;
        }
        if (mBirthdayStr.isEmpty()) {
            ToastUtils.showShort("请选择出生年月");
            return;
        }
        if (mNativePlaceStr.isEmpty()) {
            ToastUtils.showShort("请选择籍贯");
            return;
        }
        if (mAreaStr.isEmpty()) {
            ToastUtils.showShort("请选择所属园区");
            return;
        }
        if (mRaceStr.isEmpty()) {
            ToastUtils.showShort("请选择民族");
            return;
        }
        if (mMaritalStr.isEmpty()) {
            ToastUtils.showShort("请选择婚姻状况");
            return;
        }
        if (mIdStr.isEmpty()) {
            ToastUtils.showShort("请输入正确的工号");
            return;
        }
        if (mPhoneStr.isEmpty() || !FormatUtils.isMobileNO(mPhoneStr)) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if (mEducationStr.isEmpty()) {
            ToastUtils.showShort("请选择学历");
            return;
        }
        // 打包数据
        PersonalInfo info = new PersonalInfo();
        info.setIdNo(mIdStr);
        info.setPhoneNo(mPhoneStr);
        info.setName(mNameStr);
        if (mSexStr.equals("女")) {
            info.setSex("0");
        } else {
            info.setSex("1");
        }
        info.setNationality(mRaceStr);
        if (mMaritalStr.equals("已婚")){
            info.setMarried(true);
        } else {
            info.setMarried(false);
        }
        info.setBirthDate(mBirthdayStr);
        info.setEducation(mEducationStr);
        info.setGraduateTime(mGraduateTimeStr);
        info.setCollege(mGraduateSchoolStr);
        info.setSpecialty(mMajorStr);
        info.setNativePlace(mNativePlaceStr);
        info.setParkCode(mAreaStr);
        info.setGrowthExp(mLifeExperienceStr);

        // 发送网络请求
        mRxManager.add(ApiRequest
                .requestCommitInfo(mUserInfo.getJwt(), info)
                .subscribe(new RxSubscriber<String>(mContext) {
                    @Override
                    protected void _onNext(String msg) {
                        // 响应处理
                        if (GsonUtils.getIntValue(msg, "error") == 0) {
                            ToastUtils.showShort("提交个人资料成功");
                            // 跳转到开始测评页面
                            ActTransAnimUtils.startActionSlideRightToLeft(FillInfoActivity.this, StartTestActivity.class);
                            finish();
                        } else {
                            String reason = GsonUtils.getValue(msg, "reason");
                            ApiConstants.showError(reason);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        // TODO:处理失败响应
                        ToastUtils.showShort("提交个人资料失败:" + message);
                    }
                }));
    }

    /*--------------------------------------------------------------------------------
     *                          仿iOS的PickerView组件集合
     ---------------------------------------------------------------------------------*/
    /**
     * Des:构建选择器(出生年月)
     */
    private FlatTimePickerView buildBirthdayPickerView() {
        FlatTimePickerView pvTime = new FlatTimePickerView
                .Builder(this, new FlatTimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mBirthdayEdit.setText(DateFormat.format("yyyy年MM月", date));
                    }
                }, 0)
                .build();

        return pvTime;
    }

    /**
     * Des:构建选择器(毕业时间)
     */
    private FlatTimePickerView buildGraduateTimePickerView() {
        FlatTimePickerView pvTime = new FlatTimePickerView
                .Builder(this, new FlatTimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mGraduateTimeEdit.setText(DateFormat.format("yyyy年MM月", date));
                    }
                }, 0)
                .build();

        return pvTime;
    }

    /**
     * Des:构建选择器(性别)
     */
    private FlatOptionsPickerView buildSexPickerView() {
        FlatOptionsPickerView pvOptions = new FlatOptionsPickerView
                .Builder(this, new FlatOptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        mSexEdit.setText(mSexList.get(options1));
                    }
                }, 0)
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvOptions.setPicker(mSexList);//添加数据源
        return pvOptions;
    }

    /**
     * Des:构建选择器(民族)
     */
    private FlatOptionsPickerView buildRacePickerView() {
        FlatOptionsPickerView pvOptions = new FlatOptionsPickerView
                .Builder(this, new FlatOptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        mRaceEdit.setText(mRaceList.get(options1));
                    }
                }, 0)
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvOptions.setPicker(mRaceList);//添加数据源
        return pvOptions;
    }

    /**
     * Des:构建选择器(学历)
     */
    private FlatOptionsPickerView buildEducationPickerView() {
        FlatOptionsPickerView pvOptions = new FlatOptionsPickerView
                .Builder(this, new FlatOptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        mEducationEdit.setText(mEducationList.get(options1));
                    }
                }, 0)
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvOptions.setPicker(mEducationList);//添加数据源
        return pvOptions;
    }

    /**
     * Des:构建选择器(籍贯)
     */
    private FlatOptionsPickerView buildNativePlacePickerView() {
        FlatOptionsPickerView pvOptions = new FlatOptionsPickerView
                .Builder(this, new FlatOptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        mNativePlaceEdit.setText(mNativePlaceList.get(options1));
                    }
                }, 0)
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvOptions.setPicker(mNativePlaceList);//添加数据源
        return pvOptions;
    }

    /**
     * Des:构建选择器(所属园区)
     */
    private FlatOptionsPickerView buildAreaPickerView() {
        FlatOptionsPickerView pvOptions = new FlatOptionsPickerView
                .Builder(this, new FlatOptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        mAreaEdit.setText(mAreaList.get(options1));
                    }
                }, 0)
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvOptions.setPicker(mAreaList);//添加数据源
        return pvOptions;
    }

    /**
     * Des:构建选择器(婚姻)
     */
    private FlatOptionsPickerView buildMaritalPickerView() {
        FlatOptionsPickerView pvOptions = new FlatOptionsPickerView
                .Builder(this, new FlatOptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        mMaritalEdit.setText(mMaritalList.get(options1));
                    }
                }, 0)
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvOptions.setPicker(mMaritalList);//添加数据源
        return pvOptions;
    }


}
